var dataUrl = null;

$(function () {
    dataUrl ="/modules";
    // dataUrl = $("#dataUrl").val();
    /* scoreLast = $("#deductionNum").val();
     deductionLast = $("#scoreNum").val();*/
    // 绑定取消按钮事件
    $(".cancel").click(function () {
        parent.layer.close(parent.pageii);
    });
    //扣分 加
    $("#deduction-num-plus").click("click", function () {
        $("#deductionNum").val(($("#deductionNum").val() * 1 + 0.1).toFixed(2));
    });
    //扣分 减
    $("#deduction-num-reduce").click("click", function () {
        var deductionNum = $("#deductionNum").val() * 1;
        if (deductionNum <= 0) {
            $("#deductionNum").val(0);
        } else {
            $("#deductionNum").val((deductionNum - 0.1).toFixed(2));
        }
    });
    //得分 加
    $("#score-num-plus").click("click", function () {
        $("#scoreNum").val(( $("#scoreNum").val() * 1 + 0.1).toFixed(2));
    });
    //得分 减
    $("#score-num-reduce").click("click", function () {
        var scoreNum = $("#scoreNum").val() * 1;
        if (scoreNum <= 0) {
            $("#scoreNum").val(0);
        } else {
            $("#scoreNum").val((scoreNum - 0.1).toFixed(2));
        }
        // $("#scoreNum").val(($("#scoreNum").val() * 1 - 0.1).toFixed(2));
    });
    //扣分
    $("#deduction").click("click", function () {
        $("#score").removeClass("check-btn");
        $("#deduction").addClass("check-btn");
        $("#deductionDiv").show();
        $("#scoreDiv").hide();
        $("#score-corp-div").hide();
        $("#deduction-corp-div").show();
    });
    //得分
    $("#score").click("click", function () {
        $("#deduction").removeClass("check-btn");
        $("#score").addClass("check-btn");
        $("#scoreDiv").show();
        $("#deductionDiv").hide();
        $("#score-corp-div").show();
        $("#deduction-corp-div").hide();
    });
    /*getResponsibilityCropList('mainResponsibilityCrop', "主要责任单位");
     getResponsibilityCropList('secondaryResponsibilityCrop', "次要责任单位");
     getResponsibilityCropList('relevantResponsibilityCrop', "相关责任单位");*/
    responsibility();
    $("form").validate({
        submitHandler: function (form) {// 必须写在验证前面，否则无法ajax提交
            //jquery获取复选框值
            // var chk_value =[];//定义一个数组
            var index = layer.load(1, {shade: false});
            $("#submitButton").attr("disabled", "disabled");
            var responsibilityCorpPersonIds = "";
            $(".main-responsibility").each(function () {
                var checkItemRuleId = $(this).attr("data_id");
                responsibilityCorpPersonIds +=
                    getSelectValue("mainResponsibilityCrop" + checkItemRuleId, "mainResponsibilityPerson" + checkItemRuleId,checkItemRuleId) + ",";
            });
            $(".secondary-responsibility").each(function () {
                var checkItemRuleId = $(this).attr("data_id");
                responsibilityCorpPersonIds +=
                    getSelectValue("secondaryResponsibilityCrop" + checkItemRuleId, "secondaryResponsibilityPerson" + checkItemRuleId,checkItemRuleId) + ",";
            });
            $(".relevant-responsibility").each(function () {
                var checkItemRuleId = $(this).attr("data_id");
                responsibilityCorpPersonIds +=
                    getSelectValue("relevantResponsibilityCrop" + checkItemRuleId, "relevantResponsibilityPerson" + checkItemRuleId,checkItemRuleId) + ",";;
            });
            $("#responsibilityCorpPersonIds").val(responsibilityCorpPersonIds);
            $(form).ajaxSubmit({
                dataType: "text",
                success: function (data) {
                    $("#submitButton").removeAttr("disabled");
                    layer.close(index);
                    if (data == "success") {
                        // layer.confirm('添加成功!是否关闭窗口?', function (index) {
                            // parent.grid.loadData();
                            parent.location.reload();
                            parent.layer.close(parent.pageii);
                            return false;
                        // });
                    } else {
                        layer.alert('添加失败！', 3);
                    }
                }
            });
        },
        /*  rules : {
         "sysRoleFormMap.roleName" : {
         required : true,
         maxlength : 50,
         },
         "sysRoleFormMap.roleKey" : {
         required : true,
         maxlength : 50,
         remote : { // 异步验证是否存在
         type : "POST",
         url : rootPath + '/role/isExist',
         data : {
         roleKey : function() {
         return $("#roleKey").val();
         },
         }
         }
         },
         "sysRoleFormMap.comment" : {
         maxlength : 500,
         },
         },*/
        errorPlacement: function (error, element) {// 自定义提示错误位置
            $(".l_err").css('display', 'block');
            // element.css('border','3px solid #FFCCCC');
            $(".l_err").html(error.html());
        },
        success: function (label) {// 验证通过后
            $(".l_err").css('display', 'none');
        }
    });
});
function responsibility() {
    $(".main-responsibility").each(function () {
        var checkItemRuleId = $(this).attr("data_id");
        // alert(checkItemRuleId);
        getResponsibilityCropList("mainResponsibilityCrop" + checkItemRuleId, checkItemRuleId);
    });
    $(".secondary-responsibility").each(function () {
        var checkItemRuleId = $(this).attr("data_id");
        // alert(checkItemRuleId);
        getResponsibilityCropList("secondaryResponsibilityCrop" + checkItemRuleId, checkItemRuleId);
    });
    $(".relevant-responsibility").each(function () {
        var checkItemRuleId = $(this).attr("data_id");
        // alert(checkItemRuleId);
        getResponsibilityCropList("relevantResponsibilityCrop" + checkItemRuleId, checkItemRuleId);
    });
}
function getSelectValue(corpId, personId,checkItemRuleId) {
    var corpPersonId = checkItemRuleId;
    var corpId = $("#" + corpId).val();
    var personId = $("#" + personId).val();
    corpPersonId += "-";
    if (corpId != null && corpId != "" && corpId != 0 && corpId != undefined && corpId != "null") {
        corpPersonId += corpId;
    } else {
        corpPersonId += "0";
    }
    corpPersonId += "-";
    if (personId != null && personId != "" && personId != 0 && personId != undefined && personId != "null") {
        corpPersonId += personId;
    } else {
        corpPersonId += "0";
    }
    return corpPersonId;
}
/**
 * 责任单位下拉列表
 */
function getResponsibilityCropList(id, checkItemRuleId) {
    var url = dataUrl + "/taskManage/findResponsibilityCrop?checkId=" + $("#checkId").val() + "&checkItemRuleId=" + checkItemRuleId;
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        cache:false,
        success: function (result) {
            var html = "";
            // html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "' id='corpId" +checkItemRuleId+"_"+ result[i].id + "' post_id='" + result[i].postId + "'>" + (result[i].caption) + "</option>";
                }
                $("#" + id).html(html);
                $("#" + id).trigger('change');
                //getResponsibilityPersonList(result[0].id, id)
            }
        }
    });
}
/**
 * 责任人下拉列表
 * @param cropId 单位id
 */
function getResponsibilityPersonList(corpId, id,checkItemRuleId) {
    var postId =$(" #corpId"+checkItemRuleId+"_" + corpId).attr("post_id");
    var url = dataUrl + "/corp/persons?corpId=" + corpId;
    if (parseInt(postId) != -1) {
        url = dataUrl + "/taskManage/findResponsibilityPerson?checkId=" + $("#checkId").val() + "&corpId=" + corpId + "&postId=" + postId;
    }
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        cache:false,
        success: function (result) {
            var html = "";
            // html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].name) + "</option>";
                }
                // $("#" + id).html(html);
            }
            $("#" + id).html(html);
        }
    });
}